package com.hyt.it.ogt.kq.service.gov.schedule.statics;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ScheduleResult;
import com.hyt.it.ogt.kq.service.gov.schedule.statics.ScheduleStaticResult.BatchStaticResult;
import com.hyt.it.ogt.kq.service.gov.schedule.utils.ScheduleUtils;
import com.hyt.it.ogt.kq.service.gov.schedule.impl.ExamRoomScheduleImpl.PackageExamRoomCandidate;
import com.hyt.it.ogt.kq.service.gov.schedule.model.Candidate;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ExamInfo;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ExamRoom;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ScheduleConstrain;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ScheduleExamRoom;

/**
 * 编排信息统计功能
 */
public class ScheduleStatics {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleStatics.class);

    /**
     * 编排结果统计
     */
    public static ScheduleStaticResult totalStaticSchedule(ScheduleConstrain scheduleConstrain,
            List<ScheduleResult> scheduleResults,
            List<ExamRoom> examRooms) {

        Map<String, ExamRoom> examRoomMap = examRooms.stream()
                .collect(Collectors.toMap(ExamRoom::getRoomId, item -> item));

        ScheduleStaticResult scheduleStaticResult = ScheduleStaticResult.builder()
                .batchStaticResults(new ArrayList<>())
                .build();
        // 总编排次数统计
        int totalBatchNumber = scheduleConstrain.getBatchNumber();
        scheduleStaticResult.setTotalBatchNumber(totalBatchNumber);
        logger.info("编排批次数 :{} 已编排批次数 :{}", totalBatchNumber, scheduleResults.size());

        int totalExamRoom = examRooms.size();
        int totalExamRoomCapacity = examRooms.stream().mapToInt(ExamRoom::getCapacity).sum();
        scheduleStaticResult.setTotalExamRoom(totalExamRoom);
        scheduleStaticResult.setTotalExamRoomCapacity(totalExamRoomCapacity);
        logger.info("参与编排考场总数： {}, 考场总容量 :{}", totalExamRoom, totalExamRoomCapacity);

        for (ScheduleResult scheduleResult : scheduleResults) {
            int totalScheduleCandidate = 0;
            int totalScheduleExamRoomCapacity = 0;

            for (ScheduleExamRoom examRoom : scheduleResult.getExamRooms()) {
                totalScheduleCandidate = totalScheduleCandidate + examRoom.getCandidates().size();
                totalScheduleExamRoomCapacity = totalScheduleExamRoomCapacity
                        + examRoomMap.get(examRoom.getRoomId()).getCapacity();
            }

            int totalScheduleExamRoom = scheduleResult.getExamRooms().size();

            int totalCandidate = 0;
            for (PackageExamRoomCandidate packageExamRoomCandidate : scheduleResult.getPackageGroupMap().values()) {
                List<List<Candidate>> candidateLists = packageExamRoomCandidate.getCandidates();
                for (List<Candidate> candidates : candidateLists) {
                    totalCandidate = totalCandidate + candidates.size();
                }
            }

            BatchStaticResult batchStaticResult = BatchStaticResult.builder().batchNO(scheduleResult.getBatchNo())
                    .batchTotalCandidateNumber(totalCandidate)
                    .batchTotalScheduleCandidateNumber(totalScheduleCandidate)
                    .batchTotalExamRoomCapacity(totalScheduleExamRoomCapacity)
                    .batchTotalExamRoomNumber(totalScheduleExamRoom)
                    .build();

            scheduleStaticResult.getBatchStaticResults().add(batchStaticResult);
            logger.info("考试批次：{} 编排总考场数: {}, 已编排考场数 :{}", scheduleResult.getBatchNo(), totalExamRoom,
                    totalScheduleExamRoom);
            logger.info("考试批次: {} 编排总考生: {}, 已编排考生: {}", scheduleResult.getBatchNo(), totalCandidate,
                    totalScheduleCandidate);
        }
        return scheduleStaticResult;
    }

    public static ScheduleStaticDetail showScheduleDetail(ScheduleConstrain scheduleConstrain,
            List<ScheduleResult> scheduleResults,
            List<ExamRoom> examRooms, List<ExamInfo> examInfos) {
        Map<String, ExamRoom> examRoomMap = examRooms.stream()
                .collect(Collectors.toMap(ExamRoom::getRoomId, item -> item));
        Map<String, ExamInfo> examInfoMap = examInfos.stream()
                .collect(Collectors.toMap(ExamInfo::getBatchId, item -> item));
        ScheduleStaticDetail scheduleStaticDetail = ScheduleStaticDetail.builder().build();

        scheduleResults.forEach(item -> {
            ExamInfo examInfo = examInfoMap.get(item.getBatchNo());
            Map<String, Candidate> candidateMap = ScheduleUtils.getExamCandidateMap(examInfo);
            item.getExamRooms().forEach(room -> {
                room.getCandidates().forEach(candidate -> {
                    if (!scheduleStaticDetail.getCandidateDetails().containsKey(candidate.getBmStudentId())) {
                        scheduleStaticDetail.getCandidateDetails().put(candidate.getBmStudentId(), new ArrayList<>());
                    }
                    ExamRoom examRoom = examRoomMap.get(candidate.getRoomId());
                    Candidate candidateInfo = candidateMap.get(candidate.getBmStudentId());
                    // batchNO, roomSchoo,roomName,position, candidateSchool, candidateClass,
                    // candidateName, candidateSubject
                    String detail = String.format("%s,%s,%s,%s,%s,%s,%s", item.getBatchNo(), examRoom.getCity(),
                            examRoom.getRoomId(), candidate.getPosition(), candidateInfo.getLevelFour(),
                            candidateInfo.getName(), candidateInfo.getSubject());
                    scheduleStaticDetail.getCandidateDetails().get(candidate.getBmStudentId()).add(detail);
                });
            });
        });

        return scheduleStaticDetail;
        // Map<String, ExamRoom> examRoomMap = examRooms.stream()
        // .collect(Collectors.toMap(ExamRoom::getRoomId, item -> item));
        // scheduleResults.forEach(item -> {
        // item.getExamRooms().forEach(room -> {
        // logger.info("编排任务: {} 考试批次: {} 考场组织机构 :{}, 考场ID:{}, 考试科目:{} 考场容量: {} 考场人数:
        // {}",
        // item.getScheduleTaskId(), item.getBatchNo(), room.getGroupName(),
        // room.getRoomId(),
        // StringUtils.join(room.getSubjects(), "|"),
        // examRoomMap.get(room.getRoomId()).getCapacity(),
        // room.getCandidates().size());
        // });
        // });
    }
}
